package com.mypage.view.activity.beautMore;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mypage.adapter.NsmallGroupAdapter;
import com.mypage.bean.QuanXian;
import com.mypage.bean.SmallGroup;
import com.mypage.utils.AnimViewUtils;
import com.mypage.utils.SaveTemporaryData;
import com.mypage.utils.UtilsShowDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BusinChangeActivity extends BaseActivity {


    public String layoutId;
    public String recordId;
    public String relatedlistId;
    public String prefix;
    @Bind(R.id.showOrhint)
    TextView showOrhint;
    @Bind(R.id.lineView)
    View lineView;


    private String mEns = RunTimeManager.getInstance().getlanguage();
    @Bind(R.id.imgLeftBack)
    ImageView imgLeftBack;
    @Bind(R.id.listGroup)
    ListView listGroup;
    @Bind(R.id.btnTextCreate)
    TextView btnTextCreate;
    @Bind(R.id.topSynchLayout)//同步标志
            RelativeLayout topSynchLayout;
    /**
     * toast   toast_hint_beau.xml蓝色的
     */
    @Bind(R.id.toastHintContent)
    TextView toastHintContent;
    @Bind(R.id.imgDeletes)
    ImageView imgDeletes;
    @Bind(R.id.layoutTop)
    FrameLayout layoutTop;
    @Bind(R.id.topLayoutBackg)
    RelativeLayout topLayoutBackg;
    @Bind(R.id.textNumberRecord)
    TextView textNumberRecord;


    private UtilsShowDialog showDialog;
    private String mData;
    private List<SmallGroup.GroupData.Sgroup> mList;
    private NsmallGroupAdapter mAdapter;

    private String mUserName;

    private LayoutAnimationController lac;
    private ScaleAnimation sa;
    private AlphaAnimation appearAnimator;
    private AnimationSet set;
    public static boolean mToastBool = true;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.busin_change);
        ButterKnife.bind(this);
        initeView();
//        topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
        getData();
//        getHttpData();
        initListener();
        getQuanXian();
    }

    @Override
    public void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.busin_change;
    }
    public void initeView() {
        set = new AnimationSet(true);
        topLayoutBackg.setVisibility(View.GONE);
        sa = new ScaleAnimation(0, 1, 0, 1);
        sa.setDuration(400);


        appearAnimator = new AlphaAnimation(0, 1);
        appearAnimator.setDuration(400);
        set.addAnimation(sa);
        set.addAnimation(appearAnimator);
        set.setDuration(400);
        lac = new LayoutAnimationController(set, 0.5f);
    }

    @Override
    protected void onResume() {
        showDialog = new UtilsShowDialog(this);
        if ("en".equals(mEns)) {
            showDialog.onShowLoadingDialog("Updating...");
        } else {
            showDialog.onShowLoadingDialog("正在更新...");
        }
        getHttpData();
        if (mToastBool) {
            layoutTop.setVisibility(View.GONE);
            setToast();
        } else {
            topLayoutBackg.setVisibility(View.GONE);
            layoutTop.setVisibility(View.GONE);
        }

        super.onResume();
    }

    private void getData() {

        layoutId = getIntent().getStringExtra("layoutId");
        recordId = getIntent().getStringExtra("recordId");
        relatedlistId = getIntent().getStringExtra("relatedlistId");
        prefix = getIntent().getStringExtra("prefix");
    }

    @OnClick(R.id.imgLeftBack)
    public void imgLeftBack() {
        finish();
    }

    @OnClick(R.id.btnTextCreate)
    public void btnTextCreate() {
        Intent intent = new Intent(BusinChangeActivity.this, AddGroupPersonActivity.class);
        intent.putExtra("recordId", recordId);
        intent.putExtra("mPersionName", getIntent().getStringExtra("mPersionName"));
        startActivity(intent);
    }

    private void getHttpData() {
        createJson();
        HttpUtils utils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("binding", RunTimeManager.getInstance().getServerBinding());
        params.addBodyParameter("serviceName", "getRelationDataByRelationId");
        params.addBodyParameter("data", mData);//对象id
        Log.d("request业务机会小组url", UrlManager.getInterfaceUrl() + "?binding=" + RunTimeManager.getInstance().getServerBinding() + "&data=" + mData + "&serviceName=getRelationDataByRelationId");
        utils.send(HttpRequest.HttpMethod.POST, UrlManager.getInterfaceUrl(), params, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                Log.d("request业务机会小组Success", "异常类型" + arg0.toString() + "；；；失败信息" + arg1);
                showDialog.onDismissLoadingDialog();
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                Log.d("request业务机会小组Success", arg0.result.toString());
                dispose(arg0.result.toString());
            }
        });
    }


    // 把数据转换成json格式。用于上传服务端
    public void createJson() {
        JSONObject object = new JSONObject();

        try {
            object.put("layoutId", layoutId);
            object.put("recordId", recordId);
            object.put("relatedlistId", relatedlistId);
            mData = object.toString();

            System.out.println(object);

        } catch (JSONException e) {

            e.printStackTrace();
        }
    }

    public void dispose(String s) {
        showDialog.onDismissLoadingDialog();
        SmallGroup mCallBackData = new Gson().fromJson(s, SmallGroup.class);
        mList = mCallBackData.data.relationData;
        mAdapter = new NsmallGroupAdapter(this, mList);
        if (mList.size() == 0) {
            showOrhint.setVisibility(View.VISIBLE);
            lineView.setVisibility(View.VISIBLE);
            listGroup.setVisibility(View.GONE);
            return;
        }
        showOrhint.setVisibility(View.GONE);
        lineView.setVisibility(View.GONE);
        listGroup.setVisibility(View.VISIBLE);
        textNumberRecord.setText(String.valueOf(mList.size()));
        listGroup.setAdapter(mAdapter);
        listGroup.setLayoutAnimation(lac);
        mAdapter.notifyDataSetChanged();
    }

    public void initListener() {
        listGroup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(BusinChangeActivity.this, GroupPdetailActivity.class);
                intent.putExtra("mCreateId", mList.get(position).createbyid);
                intent.putExtra("mOpport", mList.get(position).ownerid);

                intent.putExtra("mId", mList.get(position).id);//用户Id
                intent.putExtra("mOpportunity", mList.get(position).opportunityid);
                intent.putExtra("mUserid", mList.get(position).userid);

                intent.putExtra("mName", mList.get(position).username);//用户名字

                intent.putExtra("mQuanXian", mList.get(position).opportunityaccesslevel);//权限
                intent.putExtra("mZhiWei", mList.get(position).teammemberrole);//职位

                intent.putExtra("mCreate", mList.get(position).createbyname);//创建人
                intent.putExtra("mCreateTime", mList.get(position).createdate);//创建时间
                intent.putExtra("mAppor", mList.get(position).opportname);//业务机会
                mUserName = mList.get(position).username;
                startActivity(intent);
            }
        });
    }

    @OnClick(R.id.imgDeletes)
    public void onViewClicked() {
        layoutTop.setVisibility(View.GONE);
    }


    public void setToast() {
        layoutTop.setVisibility(View.VISIBLE);
        topLayoutBackg.setVisibility(View.GONE);
        if ("smart".equals(SaveTemporaryData.mSmart)) {
            if ("".equals(SaveTemporaryData.mToastStr)) {
                toastHintContent.setText(getString(R.string.uwxzcy) + mUserName + getString(R.string.yibaocun));//成功
                topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
            } else {
                toastHintContent.setText(SaveTemporaryData.mToastStr);//失败
            }

            layoutTop.setVisibility(View.VISIBLE);
            new AnimViewUtils().appearToast(topLayoutBackg);
        } else if ("delete".equals(SaveTemporaryData.mSmart)) {


            if ("".equals(SaveTemporaryData.mToastStr)) {
                toastHintContent.setText(getString(R.string.uwxzcy) + mUserName + getString(R.string.yishanchu));//成功
                topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
            } else {
                toastHintContent.setText(SaveTemporaryData.mToastStr);//失败
            }


            layoutTop.setVisibility(View.VISIBLE);
            new AnimViewUtils().appearToast(topLayoutBackg);

        } else if ("create".equals(SaveTemporaryData.mSmart)) {


            toastHintContent.setText(SaveTemporaryData.mToastStr);//成功
            topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));

            layoutTop.setVisibility(View.VISIBLE);
            new AnimViewUtils().appearToast(topLayoutBackg);


        }

    }

    @Override
    protected void onPause() {
        SaveTemporaryData.mToastStr = null;
        SaveTemporaryData.mSmart = null;
        mToastBool = false;
        super.onPause();
    }

    public QuanXian mCallBackData;

    public void getQuanXian() {
        HttpUtils utils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("binding", RunTimeManager.getInstance().getServerBinding());
        params.addBodyParameter("serviceName", "getObjectPermissionByPrefix");
        params.addBodyParameter("prefix", prefix);//对象id
        Log.d("request业务机会小组url", UrlManager.getInterfaceUrl() + "?binding=" + RunTimeManager.getInstance().getServerBinding() + "&prefix=" + "prefix" + "&serviceName=getObjectPermissionByPrefix");
        utils.send(HttpRequest.HttpMethod.POST, UrlManager.getInterfaceUrl(), params, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                Log.d("request权限Success", "异常类型" + arg0.toString() + "；；；失败信息" + arg1);

            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                Log.d("reques权限Success", arg0.result.toString());
                mCallBackData = new Gson().fromJson(arg0.result.toString(), QuanXian.class);
            }
        });
    }

}
